package com.xigua.JucOfBeautiful.chapterOne.threadDeadLock1_9;

/**
 * @author LiRongHua
 * @Title: ${file_name}
 * @Package ${package_name}
 * @Description: 死锁产生的必要条件 互斥条件、持有并等待、不可剥夺条件、环路等待
 * @date 2022/1/2316:20
 */
public class DeadLcokTest1 {

    private static Object resoucreA = new Object();
    private static Object resoucreB = new Object();

    public static void main(String[] args) {
        Thread threadA = new Thread(()->{
            synchronized (resoucreA){
                System.out.println(Thread.currentThread()+" get resourceA");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+" try get resourceB");
                synchronized (resoucreB){
                    System.out.println(Thread.currentThread()+" get resourceB");
                    resoucreB.notify();
                }
            }

        },"threadA");

        Thread threadB = new Thread(()->{
            synchronized (resoucreB){
                System.out.println(Thread.currentThread()+" get resourceB");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread()+" try get resourceA");
                try {
                    resoucreB.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resoucreA){
                    System.out.println(Thread.currentThread()+" get resourceA");
                }
            }
        },"threadB");

        threadA.start();
        threadB.start();

    }

}
